package cn.itsource.org.controller;


import cn.itsource.basic.util.AjaxResult;

import cn.itsource.org.domain.TEmployee;

import cn.itsource.org.query.TEmployeeQuery;
import cn.itsource.org.service.TEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@Validated
@RequestMapping("/employee")

public class TEmployeeController {
    @Autowired
    private TEmployeeService tEmployeeService;

    /**
     * 增加或修改
     */
    @PutMapping("/save")
    public AjaxResult save(@RequestBody @Validated TEmployee tEmployee)
    {
        try {
            tEmployeeService.save(tEmployee);
            return new AjaxResult().me().setSuccess(true).setMessage("更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().me().setSuccess(false).setMessage("更新失败");
        }
    }

    /**
     * 根据id删除
     * @param id
     */
    @DeleteMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable("id") Long id)
    {
        try {
            tEmployeeService.delete(id);
            return new AjaxResult().me().setSuccess(true).setMessage("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().me().setSuccess(false).setMessage("删除失败");
        }
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */

    @GetMapping("/selectById/{id}")
    public AjaxResult selectById(@PathVariable("id") Long id)
    {
        try {
            return new AjaxResult().me().setResultObj(tEmployeeService.selectById(id));

        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("服务器异常");
        }

    }

    /**
     * 查询所有
     */
    @PatchMapping("/selectAll")
    public AjaxResult selectAll()
    {
        try {
            return new AjaxResult().me().setResultObj(tEmployeeService.selectAll());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("服务器异常");
        }
    }

    /**
     * 批量删除
     *
     * @return
     */
    @PatchMapping("/patchDelete")
    public AjaxResult patchDelete(@RequestBody List<Long> ids)
    {
        try {
            tEmployeeService.patchDelete(ids);
            return new AjaxResult().me().setMessage("删除成功").setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("服务器异常");
        }
    }

    /**
     * 分页查询
     */

    @PostMapping("/selectByQueryPage")
    public AjaxResult selectByPage(@RequestBody TEmployeeQuery tEmployeeQuery)
    {
        try {
            return new AjaxResult().me().setResultObj(tEmployeeService.selectByQueryPage(tEmployeeQuery));
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("服务器异常");
        }
    }



}
